<?php 

namespace Home\Controller;
use Think\Controller;
/**
* 登陆功能块
*/
class LoginController extends Controller
{
	
	public function index(){
		layout(false);
		if (IS_POST) {

			$username = I('post.user_name');
			$sql = "SELECT * from `user` WHERE `username` = '$username'";
			$user = M('user');
			$query = $user -> query($sql);

			if(md5(I('post.password'))==$query[0]['password']){
				//用户名
				$_SESSION['username']=$query[0]['username'];
				//用户id
				$_SESSION['id']=$query[0]['id'];
				//用户真实姓名
				$_SESSION['truename']=$query[0]['truename'];
				//分组
				$_SESSION['gid']=$query[0]['gid'];
				//头像
				$_SESSION['head_img']=$query[0]['head_img'];
				//电子邮箱
				// $_SESSION['email']=$query['email'];
				//积分
				// $_SESSION['score']=$query[0]['score'];	
					
				if(I('post.net_auto_login')==1){

					cookie('username','$username',604800);//设置7天cookie
					cookie('password',I('post.password'),604800);

				}
				
				
				$this->success('登陆成功！',U('Home/Setting/people'));
	
				

			}else{

				$error = "alert('您的密码有错，请重新输入')";

				$this->assign('username', $username);
				$this->assign('error', $error);
				$this->display();
				


			}

			

			
		}else{
			$this->display();
		}
		


	}

	public function loginout(){
		session_unset();
		session_destroy();
		$this->success('',U('/Home/'),0);

	}

	public function check_ajax(){

		if (IS_POST) {
			
			$username = I('post.username');
			$user = M('user');
			$sql = "SELECT * FROM `user` WHERE `username` = '$username'";

			if(!$user->query($sql)){
				$data = 1;
				$this->ajaxReturn($data, 'json');
			}	
		}
	
	}

}













 ?>